

#include "campaignintrolayer.h"
#include "gamescene.h"
#include "resourcemanager.h"
#include "log.h"
#include "gamemanager.h"
#include "cocos-ext.h"
#include "gamedatamanager.h"
#include "messagequeue.h"

CampaignIntroLayer::CampaignIntroLayer() :
campaignFormation(nullptr)
, _intro(nullptr)
, _dialogBg(nullptr)
{

}

CampaignIntroLayer::~CampaignIntroLayer() {

}

bool CampaignIntroLayer::init() {
	if (!CCLayer::init()) {
		return false;
	}

	const CCPoint ARR_POS[] = {
			ccp(248-130, 56),
			ccp(406-130, 56),
			ccp(566-130, 56),
			ccp(725-130, 56),
	};

	cocos2d::extension::CCScale9Sprite *sprFrame = cocos2d::extension::CCScale9Sprite::create("textures/frame.png", CCRectMake(0, 0, 96, 96), CCRectMake(32, 32, 32, 32));
	sprFrame->setContentSize(CCSizeMake(GM->minscale * 720, GM->minscale * 280));
	sprFrame->setPosition(XY_SCALE(480, 288));
	sprFrame->setAnchorPoint(ccp(0.5f, 0.5f));
	addChild(sprFrame);

	cocos2d::extension::CCScale9Sprite *sprBg = cocos2d::extension::CCScale9Sprite::create("textures/pad_bg.png", CCRectMake(0, 0, 225, 225), CCRectMake(100, 100, 25, 25));
	sprBg->setContentSize(CCSizeMake(GM->minscale * 698, GM->minscale * 256));
	sprBg->setPosition(XY_SCALE(480, 288));
	sprBg->setAnchorPoint(ccp(0.5f, 0.5f));
	addChild(sprBg);

	_dialogBg = sprBg;

	CCSprite* sprTitle = MAKE_SPRITE("dialog_header.png");
	sprTitle->setPosition(ccp(349, 260));
	sprTitle->setScale(1.0f);
	sprBg->addChild(sprTitle);

	_title = RM->makeButtonLabel("", 50, sprTitle);

	for(int i = 0; i < FORMATION_COUNT; i++) {
		auto btnPlayGame = RM->makeButton("button/blue_button_0.png", "button/blue_button_1.png", nullptr, this, menu_selector(CampaignIntroLayer::onPlayGame), sprBg);
		btnPlayGame->setPosition(ARR_POS[i]);
		btnPlayGame->setTag(100 + i);

		sprintf(buf, "%d", i + 1);
		RM->makeButtonLabel(buf, 24, btnPlayGame);
	}

	return true;
}

void CampaignIntroLayer::onClose(CCObject *sender) {
	getParent()->removeFromParent();

	SEND_MSG_ARGS(MessageTarget::model_layer, MessageTarget::campaign_scene, MessageFlag::model_layer_closed, 0);
}

void CampaignIntroLayer::keyBackClicked() {
	onClose(nullptr);
}

void CampaignIntroLayer::keyMenuClicked() {

}

void CampaignIntroLayer::onPlayGame(CCObject *sender) {
	int formationIndex = ((CCMenuItemSprite*)sender)->getTag() - 100;
	FormationInfo* f = GDM->formations[formationIndex];

	GameScene* scene = GameScene::create();
	scene->setFormations(f, campaignFormation);
	scene->previousScene = MessageTarget::campaign_scene;
	CCDirector::sharedDirector()->popToRootScene();
	CCDirector::sharedDirector()->replaceScene(scene);
}

void CampaignIntroLayer::setCampaignInfo(const char *title, const char *text) {
	_title->setString(title);

	CCLabelTTF* labelIntro = CCLabelTTF::create(text, "", 24);
	labelIntro->setColor(ccc3(92, 61, 25));
	labelIntro->setPosition(ccp(43, 188));
	labelIntro->setAnchorPoint(ccp(0.0f, 1.0f));
	labelIntro->setContentSize(CCSize(622,92));
	_dialogBg->addChild(labelIntro);
}
